<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');
get_config_keytable("workclass_add");
get_union_add();
empty($do) && $do = 'list';
if ($do == 'list') {
	//获取模板
	$tpl = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_template  WHERE id = '".$_GET['worktemplate']."' and unionid='".union_id."' ");
	//生成流水号
	$number=get_date('YmdHis',PHP_TIME);
	if($tpl['tmpytpe']=='2'){
		//读取流程数据
		$tplflow = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_flow  WHERE template_id = '".$tpl['id']."' and number>1 and unionid='".union_id."' ");
		if($tplflow['id']==''){
			show_msg('无审批流程信息，不可用，请联系管理员！', 'admin.php?ac=workadd&fileurl=work');
		}
	}
	if($tpl['tempkey']=='3'){
		if($tpl['tmpaddr']==''){
			show_msg('无法读取模板文件，请联系管理员先集成模板文件！', 'admin.php?ac=workadd&fileurl=work');
		}
		$filenumber=random(6,'0123456789').get_date('ymdHis',PHP_TIME);
		include_once('tpl/'.trim($tpl['tmpaddr']));
	}else{
		$filenumber=random(6,'0123456789').get_date('ymdHis',PHP_TIME);
		include_once('template/add.php');
	}

}elseif($do=='tplsave'){
	//固定选项
	$number = getGP('number','P');
	$workclasstype = getGP('workclasstype','P');
	$worktemplate = getGP('worktemplate','P');
	$title = getGP('title','P');
	$uid = $_USER->id;
	$date=get_date('Y-m-d H:i:s',PHP_TIME);
	//获取数据
	//获取表单
	//写入主表信息
	$workclass = array(
		'number' => $number,
		'workclasstype' => $workclasstype,
		'worktemplate' => $worktemplate,
		'title' => $title,
		'uid' => $uid,
		'date' => $date,
		'unionid' => union_id
	);
	insert_db('workclass',$workclass);
	$id=$db->insert_id();
	//更新附件
	$fileoffice = array(
		'officeid' => $id
	);
	update_db('fileoffice',$fileoffice, array('number' =>$_POST['filenumber']));
	//写入单项数据
	global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_form where type_id='".$workclasstype."' and template_id='".$worktemplate."' and type!='6' and unionid='".union_id."'  ORDER BY id Asc");
	while ($row = $db->fetch_array($query)) {
			$workclass_db = array(
			'formname' => $row["formname"],
			'inputname' => $row["inputname"],
			'inputvalue' => getGP(''.$row["inputname"].'','P'),
			'type' => $row["type"],
			'inputtype' => $row["inputtype"],
			'inputvaluenum' => $row["inputvaluenum"],
			'confirmation' => $row["confirmation"],
			'workclassid' => $id,
			'uid' => $uid,
			'date' => $date,
			'unionid' => union_id
		);
		insert_db('workclass_db',$workclass_db);
	
	}
	//写入数组
	global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_form where type_id='".$workclasstype."' and template_id='".$worktemplate."' and type='6' and unionid='".union_id."'  ORDER BY id Asc");
	while ($row = $db->fetch_array($query)) {
			$inputvalue=getGP(''.$row["inputname"].'','P','array');
			$workclass_db = array(
			'formname' => $row["formname"],
			'inputname' => $row["inputname"],
			'inputvalue' => serialize($inputvalue),
			'type' => $row["type"],
			'inputtype' => $row["inputtype"],
			'inputvaluenum' => $row["inputvaluenum"],
			'confirmation' => $row["confirmation"],
			'workclassid' => $id,
			'uid' => $uid,
			'date' => $date,
			'unionid' => union_id
		);
		insert_db('workclass_db',$workclass_db);
	
	}
	//主表信息
	$workclass_personnel = array(
		'name' => getGP('userkey','P'),
		'uid' => getGP('userkeyid','P'),
		'designationdate' => $date,
		'type' => '3',
		'flowid' => getGP('flowid','P'),
		'workclassid' => $id,
		'unionid' => union_id
	);
	insert_db('workclass_personnel',$workclass_personnel);
	
	//////////////////////////////////////////////////////
	if(getGP('sms_info_box_shownamemaster','P')!=''){
	$content='您有一个新"'.$title.'"需要审批,请点击查看!<a href="admin.php?ac=workkey&fileurl=work&type=3">点击审批>></a>';
	//接收人；内容；类型（1：有返回回值;0：无返回值）;URL
	SMS_ADD_POST(getGP('userkey','P'),$content,0,0,$_USER->id);
	}
	//手机短信
	if(getGP('sms_phone_box_shownamemaster','P')!=''){
	$content='您有一个新工作流编号为"'.$number.'"需要审批,请登录OA进行审批!';
	PHONE_ADD_POST(getGP('userkeyphone','P'),$content,getGP('userkey','P'),0,0,$_USER->id);
	}

	$content=$number.get_inc_log(1).$workclasstype.get_inc_log(1).$worktemplate.get_inc_log(1).$title.$date.get_inc_log(1).$uid;
	$title='新建工作流程';
	get_oa_log_dbadd($id,$content,$title,35,$_USER->id);
	show_msg($title.' 审批流程提交成功！', 'admin.php?ac=index&fileurl=work');
	
}elseif ($do == 'save') {
	
	$savetype = getGP('savetype','P');
	$number = getGP('number','P');
	$workclasstype = getGP('workclasstype','P');
	$worktemplate = getGP('worktemplate','P');
	$title = getGP('title','P');
	$uid = $_USER->id;
	$date=get_date('Y-m-d H:i:s',PHP_TIME);
	//主表信息
	$workclass = array(
		'number' => $number,
		'workclasstype' => $workclasstype,
		'worktemplate' => $worktemplate,
		'title' => $title,
		'uid' => $uid,
		'date' => $date,
		'unionid' => union_id
	);
	//写入主表信息
	insert_db('workclass',$workclass);
	$id=$db->insert_id();
	//更新附件
	$fileoffice = array(
		'officeid' => $id
	);
	update_db('fileoffice',$fileoffice, array('number' =>$_POST['filenumber']));
	global $db;
	$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_form where type_id='".$workclasstype."' and template_id='".$worktemplate."' and unionid='".union_id."'  ORDER BY id Asc");
	while ($row = $db->fetch_array($query)) {
		$workclass_db = array(
		'formname' => $row["formname"],
		'inputname' => $row["inputname"],
		'inputvalue' => getGP(''.$row["inputname"].'','P'),
		'type' => $row["type"],
		'inputtype' => $row["inputtype"],
		'inputvaluenum' => $row["inputvaluenum"],
		'confirmation' => $row["confirmation"],
		'workclassid' => $id,
		'uid' => $uid,
		'date' => $date,
		'unionid' => union_id
	);
	insert_db('workclass_db',$workclass_db);
	
	}
	//主表信息
	$workclass_personnel = array(
		'name' => getGP('userkey','P'),
		'uid' => getGP('userkeyid','P'),
		'designationdate' => $date,
		'type' => '3',
		'flowid' => getGP('flowid','P'),
		'workclassid' => $id,
		'unionid' => union_id
	);
	insert_db('workclass_personnel',$workclass_personnel);
	
	//////////////////////////////////////////////////////
	if(getGP('sms_info_box_shownamemaster','P')!=''){
	$content='您有一个新"'.$title.'"需要审批,请点击查看!<a href="admin.php?ac=workkey&fileurl=work&type=3">点击审批>></a>';
	//接收人；内容；类型（1：有返回回值;0：无返回值）;URL
	SMS_ADD_POST(getGP('userkey','P'),$content,0,0,$_USER->id);
	}
	//手机短信
	if(getGP('sms_phone_box_shownamemaster','P')!=''){
	$content='您有一个新工作流编号为"'.$number.'"需要审批,请登录OA进行审批!';
	PHONE_ADD_POST(getGP('userkeyphone','P'),$content,getGP('userkey','P'),0,0,$_USER->id);
	}

	$content=$number.get_inc_log(1).$workclasstype.get_inc_log(1).$worktemplate.get_inc_log(1).$title.$date.get_inc_log(1).$uid;
	$title='新建工作流程';
	get_oa_log_dbadd($id,$content,$title,35,$_USER->id);
    show_msg($title.' 审批流程提交成功！', 'admin.php?ac=index&fileurl=work');

}

?>